//
//  XMLHomeOriginalView.swift
//  Weibo
//
//  Created by mac on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

//原创视图

class XMLHomeOriginalView: UIView {
    
    var originalViewBottom :Constraint?
    
    var homeCellViewModel :XMLHomeCellViewModel? {
    
        didSet {
        
            if let url = homeCellViewModel?.home?.user?.profile_image_url {
            
                userImageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = homeCellViewModel?.home?.user?.screen_name
            contentLabel.text = homeCellViewModel?.home?.text
            contentLabel.numberOfLines = 0
            timeLabel.text = homeCellViewModel?.timecontent
            sourceLabel.text = homeCellViewModel?.sourceContent
//            print(homeCellViewModel?.home?.source)
            originalViewBottom?.uninstall()
            if let picUrls = homeCellViewModel?.home?.pic_urls where picUrls.count > 0 {
            
                picImageView.hidden = false
                picImageView.picUrls = picUrls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(picImageView).offset(margin).constraint
                })
            }else {
            
                picImageView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLabel).offset(margin).constraint
                })
            }
            
            mbrankImageView.image = homeCellViewModel?.mbrankImage
            verifiedTypeImageView.image = homeCellViewModel?.verifiedImage
        }
    }

    // MARK:懒加载控件
    private lazy var picImageView :XMLHomePicView = {
    
        let view = XMLHomePicView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    
    //  微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubViews() {
    
//        backgroundColor = UIColor.blueColor()
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picImageView)
        
        
        userImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.leading.equalTo(self).offset(margin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(margin)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(margin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(userImageView.snp_bottom)
            make.leading.equalTo(userImageView.snp_trailing).offset(margin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(margin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView.snp_bottom).offset(margin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-margin)
            
        }
        picImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.leading.equalTo(contentLabel)
        }
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottom = make.bottom.equalTo(picImageView.snp_bottom).offset(margin).constraint
        }
        
    }
}
